package br.usp.icmc.lasdpc.gem.com;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

/**
 * A bean that represents a node: IP and Port. It has some extras as the IP in
 * String format and the InetSocketAddress object for it.
 * 
 * @author Lourenco Alves
 *
 */
public class NodeBean {

	/** The IP represented as String (X.X.X.X) */
    private String stringIp;
    /** The IP address in raw format */
    private byte[] ip;
    /** Port of the node */
    private int port;
    /** The InetSocketAddress object for this note */
    private InetSocketAddress sockAddress;

    /**
     * Create a blank object for 0.0.0.0:0
     * 
     * @throws UnknownHostException
     */
    public NodeBean() throws UnknownHostException {
        sockAddress = new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0);
        ip = sockAddress.getAddress().getAddress();
        port = sockAddress.getPort();
        stringIp = sockAddress.getAddress().getHostAddress();
    }

    /**
     * Create an object based on the IP and Port passed by parameter.
     * 
     * @param ip the IP in the format X.X.X.X
     * @param port the number for the port
     * @throws UnknownHostException
     */
    public NodeBean(String ip, int port) throws UnknownHostException {
        this.stringIp = ip;
        this.port = port;
        sockAddress = new InetSocketAddress(InetAddress.getByName(ip), port);
        this.ip = sockAddress.getAddress().getAddress();
    }

    /**
     * Create an object based on the IP and Port passed by parameter.
     * 
     * @param ip the IP in raw format
     * @param port the number for the port
     * @throws UnknownHostException
     */
    public NodeBean(byte[] ip, int port) throws UnknownHostException {
        this.ip = ip;
        this.port = port;
        sockAddress = new InetSocketAddress(InetAddress.getByAddress(ip), port);
        this.stringIp = sockAddress.getAddress().getHostAddress();
    }

    /**
     * Load the data from a given InetSocketAddress. It creates a new 
     * InetSocketAddress and assign it to sockAddress.
     * 
     * @param isa the object in order to load from
     * @throws UnknownHostException 
     */
    public void loadFromInetSockeAddress(InetSocketAddress isa) throws UnknownHostException {
        ip = isa.getAddress().getAddress();
        port = isa.getPort();
        stringIp = isa.getAddress().getHostAddress();
        sockAddress = new InetSocketAddress(InetAddress.getByAddress(ip), port);
    }

    /**
     * Get method for IP.
     * @return
     */
    public byte[] getIp() {
        return ip;
    }

    /**
     * Set method for IP
     * @param ip
     */
    public void setIp(byte[] ip) {
        this.ip = ip;
    }

    /**
     * Get method for port
     * @return
     */
    public int getPort() {
        return port;
    }

    /**
     * Set method for port
     * @param port
     */
    public void setPort(int port) {
        this.port = port;
    }

    /**
     * Get method for sockAddress
     * @return
     */
    public InetSocketAddress getSockAddress() {
        return sockAddress;
    }

    /**
     * Set method for sockAddress
     * @param sockAddress
     */
    public void setSockAddress(InetSocketAddress sockAddress) {
        this.sockAddress = sockAddress;
    }

    /**
     * Get method for stringIP
     * @return
     */
    public String getStringIp() {
        return stringIp;
    }

    /**
     * Set method for stringIP
     * @param stringIp
     */
    public void setStringIp(String stringIp) {
        this.stringIp = stringIp;
    }
}
